/*
Copyright (c) 2008  Franklin Schmidt <fschmidt@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package fschmidt.db.pool;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fschmidt.db.DbDatabase;
import fschmidt.db.DbObject;
import fschmidt.db.DbKey;
import fschmidt.db.DbObjectFactory;
import fschmidt.db.SQLRuntimeException;
import fschmidt.db.extend.DbDatabaseExt;
import fschmidt.db.extend.DbTableExt;
import fschmidt.db.extend.DbTransaction;
import fschmidt.db.extend.DbRecordExt;
import fschmidt.db.extend.FilterDatabase;
import fschmidt.db.util.OverridingProxy;
import fschmidt.util.java.Stack;
import fschmidt.util.java.ArrayStack;


final class NestedConnection {

	private static class MyOverridingProxy extends OverridingProxy<Connection,NestedConnection> {

		MyOverridingProxy() {
			super(Connection.class,NestedConnection.class);
		}

		protected InvocationHandler newInvocationHandler(Connection obj,NestedConnection override) {
			return new MyOverridingInvocationHandler(obj,override);
		}

		protected class MyOverridingInvocationHandler extends OverridingInvocationHandler {

			MyOverridingInvocationHandler(Connection obj,NestedConnection override) {
				super(obj,override);
			}

			public Object invoke(Object proxy,Method method, Object[] args)
				throws Throwable
			{
				if( !method.getName().equals("close") )
					override.setUser();
				return super.invoke(proxy,method,args);
			}

		}

	}

	private static final MyOverridingProxy factory = new MyOverridingProxy();

	private final PooledConnection pooledCon;
	final Connection proxyCon;
	final Exception initException = new Exception("connection constructed in "+Thread.currentThread());
	private boolean isClosed = false;
	private final String user;

	NestedConnection(PooledConnection pooledCon,String user) {
		this.user = user;
		this.pooledCon = pooledCon;
		proxyCon = factory.newInstance(pooledCon.con(),this);
	}

	void setUser() throws SQLException {
		pooledCon.setUser(user);
	}

	protected void checkClosed()
		throws SQLException
	{
		if( isClosed )
			throw new SQLException("connection is closed");
	}
	
	public void setAutoCommit(boolean autoCommit)
		throws SQLException
	{
		checkClosed();
		pooledCon.setAutoCommit(autoCommit);
	}

	public void commit()
		throws SQLException
	{
		checkClosed();
		pooledCon.commit();
	}

	public void rollback()
		throws SQLException
	{
		checkClosed();
		pooledCon.rollback();
	}

	public void close()
		throws SQLException
	{
		checkClosed();
		isClosed = true;
		pooledCon.close(this);
	}

	public boolean isClosed() {
		return isClosed;
	}

}
